package nc.ui.cmp.originalbalance;

import java.awt.Container;
import java.util.List;
import java.util.Map;

import nc.bs.framework.common.NCLocator;
import nc.cmp.utils.AccountProxy;
import nc.cmp.utils.CheckException;
import nc.cmp.utils.CmpUtils;
import nc.itf.tam.account.accid.IAccOriBalanceQuery;
import nc.vo.cmp.originalbalance.OriginalBalanceVO;
import nc.vo.pub.BusinessException;

public class InnerAccImportStrategy implements ImportStrategy {
	
	private IAccOriBalanceQuery query;
	
	private Container container;
	
	private AccountProxy proxy;
	
	public void importData(Container container) throws BusinessException {
		setContainer(container);
		
		List<OriginalBalanceVO> voList = getQuery().getOriginalBalance(getClientUI().getCurrentCorp());
		OriginalBalanceVO[] vos = getClientUI().getDelegate().getAll();
		
		if(CheckException.checkContionsIsNull(voList)) {
			return;
		}else {
			if(vos == null || vos.length == 0) {
				import4Inner(voList);
			}else {
				List<String> idList = CmpUtils.makeList();
				for(OriginalBalanceVO vo : vos) {
					if(vo.getPk_account() != null) {
						if(getProxy().isInnerAcc(getClientUI().getCurrentCorp(), vo.getPk_account())) {
							idList.add(vo.getPk_account());
						}
					}
				}
				Map<String, OriginalBalanceVO> map = CmpUtils.makeMap();
				for(OriginalBalanceVO vo : voList) {
					map.put(vo.getPk_account(), vo);
				}
				if(CmpUtils.isListNotNull(idList)) {
					for(String id : idList) {
						if(map.containsKey(id)) {
							map.remove(id);
						}
					}
					if(map != null && map.size() != 0) {
						import4Inner(CmpUtils.makeList(map.values()));
					}
				}else {
					import4Inner(voList);
				}
			}
		}
		getClientUI().getBillCardPanel().getBillModel().execLoadFormula();
	}

	private void import4Inner(List<OriginalBalanceVO> voList) {
		int rowCount = getClientUI().getBillCardPanel().getBillModel().getRowCount();
		for(OriginalBalanceVO vo : voList) {
			vo.setAuditing_operator(getClientUI().getCurrentPerson());
			vo.setPk_operate(getClientUI().getCurrentPerson());
			getClientUI().getBillCardPanel().getBodyPanel().addLine();
			getClientUI().getBillCardPanel().getBillModel().setBodyRowVO(vo, rowCount);
			rowCount++;
		}
	}

	public IAccOriBalanceQuery getQuery() {
		if(query == null) {
			query = NCLocator.getInstance().lookup(IAccOriBalanceQuery.class);
		}
		return query;
	}

	public Container getContainer() {
		return container;
	}

	public void setContainer(Container container) {
		this.container = container;
	}
	
	public OriginalBalanceClientUI getClientUI() {
		return (OriginalBalanceClientUI)getContainer();
	}
	
	public AccountProxy getProxy() {
		if(proxy == null) {
			proxy = CmpUtils.makeObj(AccountProxy.class);
		}
		return proxy;
	}


}
